package secondriver.ccs.parallel;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.TimeUnit;

/**
 * Author : secondriver
 */
public class TestCcsParallelCompute {

    static ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();

    static int edge = 13;

    static int step = 13;

    /**
     * 单个任务
     */
    static void singleTaskUnit() {
        ForkJoinTask<String> resultForkJoinTask = forkJoinPool.submit(new CcsComputeTaskUnit(edge, 645));
        String fileName = resultForkJoinTask.join();
        System.out.println(fileName);
    }


    /**
     * 多任务单元分组
     */
    static void multiRangeTaskUnit() {
        CcsCompute ccsCompute = new CcsCompute(edge, step, 645, 1024);
        ForkJoinTask<List<String>> forkJoinTask = forkJoinPool.submit(ccsCompute);
        List<String> fileNameList = forkJoinTask.join();
        for (String fileName : fileNameList) {
            System.out.println(fileName);
        }
    }

    static void multiTaskUnit() {
        CcsCompute ccsCompute = new CcsCompute(edge);
        ForkJoinTask<List<String>> forkJoinTask = forkJoinPool.submit(ccsCompute);
        List<String> fileNameList = forkJoinTask.join();
        for (String fileName : fileNameList) {
            System.out.println(fileName);
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        LocalDateTime start = LocalDateTime.now();

        multiTaskUnit();

        LocalDateTime end = LocalDateTime.now();

        Duration duration = Duration.between(start, end);
        System.out.println("Cost Time:" + duration.getNano() + "纳秒");

        forkJoinPool.shutdown();
        forkJoinPool.awaitTermination(1000, TimeUnit.SECONDS);
    }
}
